<template>
  <div class="top-header">
    <section>
      <h2>电商后台管理系统</h2>
      <el-icon>
        <Menu />
      </el-icon>
    </section>

    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        {{userStore.username}}<el-icon class="el-icon--right"><arrow-down /></el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="home">首页</el-dropdown-item>
          <el-dropdown-item command="logout">退出</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup lang="ts">
import { useUserStore } from "@/store";
import { Menu,ArrowDown } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
const router = useRouter();
const userStore = useUserStore();

const handleCommand = (cmd: string) => {
  switch (cmd) {
    case "home":
      router.push("/index");
      break;
    case "logout":
        // 清除token
      localStorage.removeItem("token");
    // 跳回登录页面
      router.push("/login");
    //   清除动态路由
      userStore.activeMenus.forEach((item) => {
        router.removeRoute(item.name);
      });
      router.removeRoute('home')
    //   清空pinia中menus数据
      userStore.menus = [];
      break;
  }
};
</script>

<style lang="less" scoped>
.top-header {
  display: flex;
  justify-content: space-between;
  height: 100%;
  align-items: center;

  section {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-icon {
      color: #fff;
      font-size: 26px;
    }
  }
  h2 {
    margin-right: 30px;
  }
}
</style>
